package com.molyfun.parents.activity.report.plan;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.report.adapter.PlanDetailCallListAdapter;
import com.molyfun.parents.activity.report.adapter.PlanDetailCallListCotentAdapter;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.PlanDetailCallListViewResponse;
import com.molyfun.parents.rest.bean.PlanDetailResponse;
import com.molyfun.parents.utils.UIUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/13/16.
 */
public class PlanDetailCellListView extends LinearLayout implements View.OnClickListener, PlanDetailCallListAdapter.PlanDetailCallListCallback {

    protected LayoutInflater layoutInflater;

    private Context context;
    private PlanDetailCallListAdapter mPlanDetailCallListAdapter;
    private LinearLayoutManager mLinearLayoutManager;
    @InjectView(R.id.recyclerViewRight)
    RecyclerView mRecyclerViewRight;



    private List<PlanDetailResponse.PlanDetailBean.FieldlistBean.TemplistBean> mPlanDetailtemplist;
    private String mPlanIdt;

//    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private int mCurrentPageSize = RestClient.LARGE_PAGE_SIZE;

    public PlanDetailCellListView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public PlanDetailCellListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public PlanDetailCellListView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_plan_detail_cell_list_view, this);
        ButterKnife.inject(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    public void initData(String planId, List<PlanDetailResponse.PlanDetailBean.FieldlistBean.TemplistBean> templist) {
        initData(planId, templist, false);
    }

    public void initData(String planId, List<PlanDetailResponse.PlanDetailBean.FieldlistBean.TemplistBean> templist, boolean autoLoad) {

        if (templist == null || templist.size()<0) {
            return;
        }

        mPlanIdt = planId;
        mPlanDetailtemplist = templist;


        mLinearLayoutManager = new LinearLayoutManager(context);
        mLinearLayoutManager.setAutoMeasureEnabled(true);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerViewRight.setLayoutManager(mLinearLayoutManager);
        mRecyclerViewRight.setHasFixedSize(true);

        mPlanDetailCallListAdapter = new PlanDetailCallListAdapter(context, this);
        mRecyclerViewRight.setAdapter(mPlanDetailCallListAdapter);

        mPlanDetailCallListAdapter.refreshData(mPlanDetailtemplist);

    }

    @Override
    public void onPlanDetailCallListClick(int position, final PlanDetailCallListCotentAdapter planDetailCallListCotentAdapter) {
        PlanDetailResponse.PlanDetailBean.FieldlistBean.TemplistBean templistBean = mPlanDetailtemplist.get(position);
        String targetId = templistBean.getId();
        Call<PlanDetailCallListViewResponse> call = MFPApplication.getInstance().getRestClient().getPlanDetailCallList(targetId, mPlanIdt, RestClient.DEFAULT_PAGE, mCurrentPageSize);
        call.enqueue(new Callback<PlanDetailCallListViewResponse>() {
            @Override
            public void onResponse(Call<PlanDetailCallListViewResponse> call, Response<PlanDetailCallListViewResponse> response) {
                if (response.body() != null && response.body().getData() != null) {
                    List<PlanDetailCallListViewResponse.PlanDetailTargetList.RecordsBean> recordsBeanList = response.body().getData().getRecords();
                    planDetailCallListCotentAdapter.refreshData(recordsBeanList);
//                    mCurrentPage ++;
                } else {
//                    mCurrentPage = RestClient.DEFAULT_PAGE;
                    UIUtils.showToast(context, response.body() == null ? "" : response.body().getMsg() == null ? "" : response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<PlanDetailCallListViewResponse> call, Throwable t) {
//                mCurrentPage = RestClient.DEFAULT_PAGE;
            }
        });
    }
}